package com.bluesky.jwf.example1.forms;

import com.bluesky.jwf.ComponentFactory;
import com.bluesky.jwf.ComponentFactory.ComponentType;
import com.bluesky.jwf.component.Button;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.TextField;
import com.bluesky.jwf.component.TextField.Mode;
import com.bluesky.jwf.component.event.AbstractEventHandler;
import com.bluesky.jwf.component.event.EventHandler;
import com.bluesky.jwf.component.validator.RequiredValidator;
import com.bluesky.jwf.component.window.Dialog;
import com.bluesky.jwf.component.window.ModalResult;

public class LoginDialog extends Dialog {

	private TextField usernameField;
	private TextField passwordField;
	private RequiredValidator usernameRequiredValidator;
	private RequiredValidator passwordRequiredValidator;

	private Button buttonOK;
	private Button buttonCancel;

	@Override
	public void init(String html) {

		super.init(html);

		setWidth("350px");
		setTitle("Login Dialog");

		ComponentFactory factory = ComponentFactory.getInstance();

		usernameField = (TextField) factory
				.createComponent(ComponentType.TextField);
		bodyTag.loadByJwfClass("username").replace(usernameField);

		passwordField = (TextField) factory
				.createComponent(ComponentType.TextField);
		passwordField.setMode(Mode.Password);
		bodyTag.loadByJwfClass("password").replace(passwordField);

		usernameRequiredValidator = (RequiredValidator) factory
				.createComponent("RequiredValidator");
		usernameRequiredValidator.setTarget(usernameField);

		passwordRequiredValidator = (RequiredValidator) factory
				.createComponent("RequiredValidator");
		passwordRequiredValidator.setTarget(passwordField);

		buttonOK = (Button) factory.createComponent(ComponentType.Button);
		buttonOK.setText("Login");
		loadByJwfId("ok").replace(buttonOK);

		buttonOK.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter ) {
				validate();
				if (!validateResult)
					return;

				modalResult = ModalResult.OK;

				close();

			}
		});

		buttonCancel = (Button) factory.createComponent(ComponentType.Button);
		buttonCancel.setText("Cancel");
		loadByJwfId("cancel").replace(buttonCancel);
		buttonCancel.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter ) {

				modalResult = ModalResult.Cancel;

				close();

			}
		});

	}

	public TextField getUsernameField() {
		return usernameField;
	}

	public void setUsernameField(TextField usernameField) {
		this.usernameField = usernameField;
	}

	public TextField getPasswordField() {
		return passwordField;
	}

	public void setPasswordField(TextField passwordField) {
		this.passwordField = passwordField;
	}

	public Button getButtonOK() {
		return buttonOK;
	}

	public void setButtonOK(Button buttonOK) {
		this.buttonOK = buttonOK;
	}

	public Button getButtonCancel() {
		return buttonCancel;
	}

	public void setButtonCancel(Button buttonCancel) {
		this.buttonCancel = buttonCancel;
	}

}
